<template>
  <div>
    <el-form ref="form" :rules="rules" :model="sysDictData" label-width="100px" size="small">
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="sysDictData.dictType" :disabled="true" placeholder="请输入字典类型"/>
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="sysDictData.dictLabel" placeholder="请输入数据标签" clearable/>
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="sysDictData.dictValue" placeholder="请输入数据键值" clearable/>
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number v-model="sysDictData.dictSort" placeholder="请输入显示排序"/>
      </el-form-item>
      <el-form-item label="皮肤属性" prop="dictSkin">
        <el-select v-model="sysDictData.dictSkin" style="width: 100%" placeholder="请输入皮肤属性">
          <el-option
            v-for="item in dictSkinOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="sysDictData.remark" placeholder="请输入备注" clearable/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="sysDictData.status">
          <el-radio v-for="item in statusOptions"
                    :key="item.dictValue"
                    :label="item.dictValue">
            {{ item.dictLabel }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="add">添加</el-button>
        <el-button type="warning" size="small" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import sysDictDataApi from "@/api/system/sysDictData";
import {emptyStringToNull} from "@/utils/form";

export default {
  name: "sys-dict-data-add",
  props: {
    activeType: {
      type: String,
      default: null
    }
  },
  watch: {
    activeType: {
      immediate: true,
      handler: function (newVal, oldVal) {
        this.sysDictData.dictType = newVal;
      }
    }
  },
  data() {
    return {
      //声明属性去存在放字典数据
      statusOptions: [],
      //要添加的表单数据
      sysDictData: {
        dictLabel: null,
        dictType: null,
        dictValue: null,
        dictSort: 0,
        dictSkin: null,
        remark: null,
        status: 0
      },
      dictSkinOptions: [
        {value: null, label: "默认"},
        {value: "primary", label: "主要"},
        {value: "success", label: "成功"},
        {value: "info", label: "信息"},
        {value: "warning", label: "警告"},
        {value: "danger", label: "危险"}
      ],
      //表单验证
      rules: {
        dictLabel: [
          {required: true, message: "数据标签不能为空", trigger: "blur"}
        ],
        dictValue: [
          {required: true, message: "数据键值不能为空", trigger: "blur"}
        ],
        dictSort: [
          {required: true, message: "数据排序不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    //加载字典数据->状态
    this.getDictDataByDictType();
  },
  methods: {
    // 根据字典类型加载字典数据->状态
    getDictDataByDictType() {
      sysDictDataApi.getDictDataByDictType("sys_status_type").then(res => {
        this.statusOptions = res.data
      })
    },
    resetForm() {
      this.sysDictData = {}
      this.sysDictData.dictType = this.activeType
      this.sysDictData.dictSort = 0
      this.sysDictData.status = 0
    },
    // 保存
    add() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let saveData = emptyStringToNull(this.sysDictData)
          sysDictDataApi.save(saveData).then(res => {
            this.$message.success(res.msg)
            this.$emit("after")
            this.$emit("close")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
